﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing;
using popLib;

namespace PhotoMgr.src.common
{
    public class ExDateTimePicker : DateTimePicker
    {
        Panel panel;
        TextBox textbox;
        public ExDateTimePicker()
        {
            panel = new Panel();
            panel.Height = this.Height;
            panel.Width = this.Width - 22;
            this.Controls.Add(panel);

            textbox = new TextBox();
            textbox.Top = 2;
            textbox.Left = 2;
            textbox.Height = Height;
            textbox.BorderStyle = BorderStyle.None;
            panel.Controls.Add(textbox);
            textbox.BringToFront();
            textbox.LostFocus += new EventHandler(textbox_LostFocus);
            textbox.KeyDown += new KeyEventHandler(textbox_KeyDown);
            updatePanel();
        }

        void textbox_KeyDown(object sender, KeyEventArgs e)
        {

            if (e.KeyCode == Keys.Enter)
                textbox_LostFocus(e, new EventArgs());
            if (e.KeyCode == Keys.Escape)
            {
                textbox.Clear();
            }
        }


        void textbox_LostFocus(object sender, EventArgs e)
        {
            DateTime currentDateTime = Global.GetServerDateTime();

            if (isReadOnly) return;
            if (!StringLib.isValid(textbox.Text)) return;
            string[] parts = textbox.Text.SplitByAny("/\\-.");

            int day = parts.Length > 0 ? NumberLib.getInt(parts[0]) : currentDateTime.Day;
            day = day <= 0 ? currentDateTime.Day : day;

            int mon = parts.Length > 1 ? NumberLib.getInt(parts[1]) : currentDateTime.Month;
            mon = mon <= 0 ? currentDateTime.Month : mon;

            int yea = parts.Length > 2 ? NumberLib.getInt(parts[2]) : currentDateTime.Year;
            yea = yea <= 0 ? currentDateTime.Year : yea;
            yea = yea < 100 ? yea + 2000 : yea;

            try
            {
                if (this.CustomFormat == "yyyy")
                {
                    this.Value = new DateTime(day, 1, 1);
                }
                else if (this.CustomFormat == "MM/yyyy")
                {
                    this.Value = new DateTime(mon, day, 1);
                }
                else
                {
                    this.Value = new DateTime(yea, mon, day);
                }

                panel.Hide();
            }
            catch
            {
                textbox.LostFocus -= new EventHandler(textbox_LostFocus);
                FormLib.alert("Ngày nhập không hợp lệ!!\r\nXin vui lòng nhập ngày theo định dạng sau: \r\n    ngày\\tháng\\năm\r\n    ngày-tháng-năm\r\n    ngày.tháng.năm", "Thông báo");
                textbox.LostFocus += new EventHandler(textbox_LostFocus);
                //textbox.Focus();
                textbox.Clear();
            }
        }

        private bool isReadOnly;

        public bool ReadOnly
        {
            get { return isReadOnly; }
            set
            {
                isReadOnly = value;
                updatePanel();
            }
        }

        private bool isnull;
        public bool IsNull
        {
            get { return isnull; }
            set
            {
                isnull = value;
                updatePanel();
            }
        }

        public DateTime Values
        {
            get
            {
                if (IsNull)
                {
                    return Global.NullDateTime;
                }
                else
                {
                    return Value;
                }
            }
            set
            {
                isResetValue = true;

                if ( value.Date == Global.NullDateTime)
                {
                    this.Value = Global.GetServerDateTime();
                    IsNull = true;
                }
                else
                {
                    this.Value = value;
                    IsNull = false;
                }

                isResetValue = false;

                
            }
        }


        private void updatePanel()
        {
            textbox.ReadOnly = isReadOnly;
            textbox.Text = isnull ? "" : this.Text;
            panel.Visible = isnull || isReadOnly;

            panel.BackColor = textbox.BackColor = isReadOnly ? System.Drawing.SystemColors.Control : System.Drawing.SystemColors.Window;
            panel.Width = isReadOnly ? ClientRectangle.Width : ClientRectangle.Width - 22;
            textbox.Width = panel.Width;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            updatePanel();
        }

        private bool isResetValue = false;
        protected override void OnValueChanged(EventArgs eventargs)
        {
            if (isResetValue == false)
            {
                IsNull = false;
            }
            else
            {
                updatePanel();
            }

            base.OnValueChanged(eventargs);

        }

        protected override void OnEnter(EventArgs e)
        {
            base.OnEnter(e);
            if (textbox.Visible)
            {
                textbox.Focus();
            }
        }       
    }
}
